/**
 * Created by LeungZ on 2016/8/8.
 */
import temp from './node.html';
import ctrl from './node.ctrl.js';
import './node.scss';
import tempGrp from './group/group.html';
import ctrlGrp from './group/group.ctrl.js';
import tempDtl from './detail/detail.html';
import ctrlDtl from './detail/detail.ctrl.js';
import tempPod from '../pod/pod.html';
import ctrlPod from '../pod/pod.ctrl.js';

export default angular.module('ctg-k8sconsole').config(['$stateProvider', $stateProvider=> {
    $stateProvider.state('k8s-node', {
        url: '?kDbCode',
        templateUrl: temp,
        controller: ctrl,
        controllerAs: 'list',
        abstract: true,
        resolve: {
            list: ['nodeSrv', 'ctgNotification', '$stateParams', function (nodeSrv, ctgNotification, $stateParams) {
                return nodeSrv.getNodeList({page: 1, code: $stateParams.kDbCode}).catch(function (data) {
                    ctgNotification.error('获取宿主机列表数据有误.');
                });
            }],
            nodeResource:['list', 'nodeSrv', 'ctgNotification', '$stateParams' , function (list,nodeSrv, ctgNotification, $stateParams) {
                let nodeArr = [];
                if(list){
                    angular.forEach(list.nodes, (ele)=>{
                        nodeArr.push(ele.nodeIp)
                    });
                    return nodeSrv.queryNodeResource({clusterCode:$stateParams.kDbCode,nodeips:nodeArr}).catch(function (data) {
                        ctgNotification.error('获取节点动态资源有误.');
                    });
                }
            }]

        }
    }).state('k8s-node.group', {
        url: '/k8s/node',
        templateUrl: tempGrp,
        controller: ctrlGrp,
        controllerAs: 'list',
        resolve: {
            list: ['nodeSrv', 'ctgNotification', '$stateParams', function (nodeSrv, ctgNotification, $stateParams) {
                return nodeSrv.getGroupList({code: $stateParams.kDbCode, page: 1}).catch(function (data) {
                    ctgNotification.error('获取宿主机组详情数据有误.');
                });
            }]
        }
    }).state('k8s-node-detail', {
        url: '/k8s/node/:ip?kDbCode',
        templateUrl: tempDtl,
        controller: ctrlDtl,
        controllerAs: 'dtl',
        abstract: true,
        resolve: {
            detail: ['nodeSrv', 'ctgNotification', '$stateParams', function (nodeSrv, ctgNotification, $stateParams) {
                return nodeSrv.getNodeDetail({ip: $stateParams.ip, code: $stateParams.kDbCode}).catch(function (data) {
                    ctgNotification.error('获取宿主机详情数据有误.');
                });
            }],
            resource: ['nodeSrv', 'ctgNotification', '$stateParams', function (nodeSrv, ctgNotification, $stateParams) {
                return nodeSrv.queryNodeResource({
                    nodeips: [$stateParams.ip],
                    clusterCode: $stateParams.kDbCode
                }).catch(function (data) {
                    ctgNotification.error('获取宿主机资源数据有误.');
                });
            }]
        }
    }).state('k8s-node-detail.pod', {
        url: '/pod',
        templateUrl: tempPod,
        controller: ctrlPod,
        controllerAs: 'list',
        resolve: {
            list: ['podSrv', 'ctgNotification', '$stateParams', function (podSrv, ctgNotification, $stateParams) {
                return podSrv.getListOfNode({
                    ip: $stateParams.ip,
                    page: 1,
                    code: $stateParams.kDbCode
                }).catch(function (data) {
                    ctgNotification.error('获取宿主机容器列表数据有误.');
                });
            }]
        }
    })
}]);